package com.ma.service.impl;

import com.ma.dao.CategoryDao;
import com.ma.entity.CategoryEntity;
import com.ma.service.CategoryService;
import com.ma.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryDao categoryDao;

    @Override
    public List<CategoryVo> getCategory(){

        List<CategoryEntity> categoryEntityList = categoryDao.selectAll();
        //获取一级分类
        List<CategoryEntity> categoryOneList = categoryEntityList.stream().filter(categoryEntity -> {
            return categoryEntity.getLevel() == 1;
        }).collect(Collectors.toList());

        List<CategoryVo> voList = categoryOneList.stream().map(categoryEntity -> {
            CategoryVo categoryVo = new CategoryVo();
            categoryVo.setId(categoryEntity.getId());
            categoryVo.setName(categoryEntity.getName());

            //获取每一个当前一级分类下的二级分类
            List<CategoryEntity> categoryTwoList = categoryDao.selectCategoryByPId(categoryEntity.getCId());
//                    this.baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("p_id", categoryEntity.getCId()));

            //获取每一个当前二级分类下三级分类
            List<CategoryVo.CategoryVo2> catVo2List = categoryTwoList.stream().map(categoryTwo -> {
                //封装二级分类数据
                CategoryVo.CategoryVo2 categoryVo2 = new CategoryVo.CategoryVo2();
                categoryVo2.setId(categoryTwo.getId());
                categoryVo2.setName(categoryTwo.getName());

                List<CategoryEntity> categoryThreeList = categoryDao.selectCategoryByPId(categoryTwo.getCId());
//                        this.baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("p_id", categoryTwo.getCId()));
                List<CategoryVo.CategoryVo3> catVo3List = categoryThreeList.stream().map(categoryThree -> {
                    CategoryVo.CategoryVo3 categoryVo3 = new CategoryVo.CategoryVo3();
                    categoryVo3.setId(categoryThree.getId());
                    categoryVo3.setName(categoryThree.getName());
                    return categoryVo3;
                }).collect(Collectors.toList());

                categoryVo2.setCategoryVo3List(catVo3List);
                return categoryVo2;
            }).collect(Collectors.toList());

            categoryVo.setCategoryVo2List(catVo2List);
            return categoryVo;
        }).collect(Collectors.toList());
        return voList;
    }
}
